package com.hdl.school.utils;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;

import java.io.File;
import java.util.List;

/**
 * Created by Admin on 2016/10/21.
 */
public class SystemUtil {

    private final static String EXTERAL_APP_PACKAGE="com.hdl.schoolhelper";
    private final static String EXTERAL_APP_INSTALL="com.hdl.schoolhelper.service.AutoInstallService";
    private final static String EXTERAL_APP_RESATRT="com.hdl.schoolhelper.service.RestartService";

    public static void startHelpAppService(Context context){
       Intent intent=new Intent();
        intent.setComponent(new ComponentName(EXTERAL_APP_PACKAGE,EXTERAL_APP_RESATRT));
        context.startService(intent);
    }


    public static void startExteralAutoInstall(Context context){
        Intent intent=new Intent();
        intent.setComponent(new ComponentName(EXTERAL_APP_PACKAGE,EXTERAL_APP_INSTALL));
        context.startService(intent);
    }



    public static void installNewVersion(Context context,File updateFile){
        Uri uri = Uri.fromFile(updateFile); //获取文件的Uri
        Intent installIntent = new Intent(Intent.ACTION_VIEW);
        installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        installIntent.setDataAndType(uri,
                "application/vnd.android.package-archive");//设置intent的数据类型
        context.startActivity(installIntent);
    }

    public static String getVersionName(Context context) throws Exception
    {
        // 获取packagemanager的实例
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(),0);
        String version = packInfo.versionName;
        return version;
    }


    public static int getVersionCode(Context context) throws Exception
    {
        // 获取packagemanager的实例
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(),0);
        int code = packInfo.versionCode;
        return code;
    }




    /**
     * 用来判断服务是否运行.
     * @param className 判断的服务名字
     * @return true 在运行 false 不在运行
     */
    public static boolean isServiceRunning(Context mContext, String className) {
        boolean isRunning = false;
        Log.e("isServiceRunning", className);
        ActivityManager activityManager = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList = activityManager
                .getRunningServices(60);
        if (!(serviceList.size() > 0)) {
            return false;
        }
        for (int i = 0; i < serviceList.size(); i++) {
            if (serviceList.get(i).service.getClassName().equals(className)) {
                isRunning = true;
                break;
            }
        }
        return isRunning;
    }



}
